<?php

/**
 * Library :: DataTypes :: Array :: Conversion
 *
 * Provides functions used for converting an array to various different
 * formats.
 *
 * Warlock is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 *
 * @author     David Cole <neophyte@sourcetutor.com>
 * @package    warlock
 * @subpackage library
 * @license    GNU/GPL v2
 *
 */

function array2php($array, $name, $recursion = 1)
{
    $indent  = 4;
    $data    = '';
    $first   = true;

    foreach ($array as $key => $value)
    {
        // Add a comma at the appropriate places
        if (!$first)
            $data .= ",\n";
        else
            $first = false;

        // Indent the element
        $data .= str_repeat(' ', ($indent * $recursion));

        // Append the element
        switch (true)
        {
            case is_array($value)   : $data .= '\''.$key.'\' => array('."\n".array2php($value, $name, ++$recursion)."\n".str_repeat(' ', ($indent * $recursion)).')';  break;
            case is_bool($value)    : $data .= '\''.$key.'\' => '.($value?'true':'false'); break;
            case is_numeric($value) : $data .= '\''.$key.'\' => '.$value; break;
            case is_string($value)  : $data .= '\''.$key.'\' => \''.addslashes($value).'\'';   break;
        }
    }

    if ($recursion > 1)
        return $data;
    else
        return '<?php'."\n".'$'.$name.' = array('."\n".$data."\n".');'."\n?>";
}

function array2xml($root, $array, $charset = 'UTF-8', $level = 1)
{
    $xml = '';

    if ($level == 1)
        $xml .=  '<?xml version="1.0" encoding="'.$charset.'"?>'."\n"
                .'<'.$root.'>'."\n";

    foreach ($array as $key => $value)
    {
        $key = strtolower($key);

        if (is_array($value))
        {
            $multi_tags = false;

            foreach($value as $key2 => $value2)
            {
                if (is_array($value2))
                {
                    $xml .= str_repeat("\t", $level).'<'.$key.">\n";
                    $xml .= array2xml($value2, ++$level);
                    $xml .= str_repeat("\t", $level).'</'.$key.">\n";

                    $multi_tags = true;
                }
                else
                {
                    if (trim($value2) != '')
                        $xml .= str_repeat("\t", $level).
                                '<'.$key.'>'.htmlspecialchars($value2, ENT_QUOTES, $charset).'</'.$key.'>'."\n";

                    $multi_tags = true;
                }
            }

            if ((!$multi_tags) && (count($value) > 0))
            {
                $xml .= str_repeat("\t", $level).'<'.$key.">\n";
                $xml .= array_to_xml($value, $level + 1);
                $xml .= str_repeat("\t", $level).'</'.$key>"\n";
            }
        }
        else
            if (trim($value) != '')
                $xml .= str_repeat("\t", $level).
                        '<'.$key.'>'.htmlspecialchars($value, ENT_QUOTES, $charset).'</'.$key.'>'."\n";
    }

    if ($level == 1)
        $xml .= '</'.$root.">\n";

    return $xml;
}

?>